<?php
namespace Cyfy\Modules\BasicContent\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\BasicContent\Content;
use \Cyfy\Modules\BasicContent\Module;

/**
 * Creates a new page.
 */
class AddPage extends Forms\Form{

	function structure()
	{
        $this -> permission('BasicContent', 'add new');
        
		$i = new Forms\TextBox("title", self :: t(20));
		$i -> required = true;
		$i -> maxLength = 255;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("path", self :: t(30));
		$i -> required = true;
		$i -> maxLength = 255;
		$this -> addItem($i);
		
		$i = new Forms\Select("menu", self :: t(220));
		$this -> addItem($i);
		
		$w = new Forms\WysiwygEditor("content", self :: t(40));
		$w -> required = true;
		$this -> addItem($w);
		
		$b = new Forms\Button(self :: t(210), Forms\Button::OK);
		$this -> addButton($b);		
    }
  
	function validate()
	{
		// path OK?
		if(\Cyfy\Cyfy ::  tryPath($this -> get('path')))
        {
			return true;
		}
        
        // path used
        Message :: set(self :: t(231, array($this -> get("path"))), Message::ERROR);
        return false;
	}
  
	function submit()
	{  
		// creating content
		$content = new Content($this -> get("title"), $this -> get("content"));
        $bcid = $content -> save();
		
        // if error
		if(!$bcid)
		{
            Message :: set(self :: t(251, array($this -> get("title"))), Message::ERROR);
            return false;
        }
			
        // add path
        \Cyfy\Cyfy ::  registerPath($this -> get("path"), "BasicContent", $bcid , $this -> get("title"));
        
        // add menu
        if($this -> get("menu")){
            \Cyfy\Modules\Menu\Menu :: addItem($this -> get("title"), $this -> get("path"), $this -> get("menu"));
        }

        // show message
        Message :: set(self :: t(241, array($this -> get("title"))));
        
        // redirecting
        $this -> action = $this -> get("path");
        return true;
	}
}